import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:gbk2utf8/gbk2utf8.dart';

class HttpFactory {
  ///单例模式
  HttpFactory._instance() {
    var options =
        BaseOptions(baseUrl: _host, connectTimeout: 5000, receiveTimeout: 5000);
    _dio = Dio(options);

    var optionsUrl = BaseOptions(
        connectTimeout: 5000,
        receiveTimeout: 5000,
        responseType: ResponseType.bytes); // 设置接收类型为bytes
    _dioUrl = Dio(optionsUrl);
  }

  static HttpFactory getInstance = HttpFactory._instance();

  factory HttpFactory() => getInstance;

  static const String _host = "https://api.apiopen.top/";
  Dio _dio;
  Dio _dioUrl;

  Future getData({@required String url, int page = 1, int count = 20}) async {
    Map<String, int> queryParameters = {'page': page, 'count': count};
    var response = await _dio.post(url, queryParameters: queryParameters);

    return response;
  }

  Future getHtmlUrl({@required String url}) async {
    var rs = await _dioUrl.get(
      url, options: Options(responseType: ResponseType.bytes));
    String html = gbk.decode(rs.data);

    return html;
  }
}
